package com.wuyoucar.service.impl;

import com.wuyoucar.common.ResultBean;
import com.wuyoucar.entity.TBank;
import com.wuyoucar.mapper.TBankMapper;
import com.wuyoucar.service.InTBankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TBankServiceImpl  implements InTBankService {
    @Autowired
    TBankMapper tBankMapper;
    @Override
    public ResultBean selectAllCard(Integer userid) {
        List<TBank> list = tBankMapper.selectAllCard(userid);
        if (list.size()>0){
            return new ResultBean(200,"查询成功",list);
        }else{
            return new ResultBean(404,"查询失败");
        }

    }
    //绑定银行卡
    @Override
    public ResultBean bindCard(Integer userid, String bankcard,String bankname) {
        List<TBank> list = tBankMapper.selectAllCard(userid);
        Boolean flag = false;
        for (TBank tBank : list) {
            if (  tBank.getBankcard().equals(bankcard)){
                flag = true;
                break;
            }
        }
        if (flag){
            return  new ResultBean(403,"该卡已绑定,无法重复操作");
        }else{
            TBank tBank = new TBank(userid,bankcard);
            int n = tBankMapper.bindCard(userid,bankcard,bankname);
            if (n>0){
                return new ResultBean(200,"操作成功");
            }else{
                return new ResultBean(404,"操作失败");
            }
        }
    }
    //给用户打款
    @Override
    public ResultBean updateMoney(String bankcard, Double money) {
        TBank bank = new TBank(bankcard,money);
        int i = tBankMapper.updateMoney(bank);
        if (i > 0){
            return new ResultBean(200,"打款成功");
        }else {
            return new ResultBean(404,"打款失败");
        }
    }
    //解绑
    @Override
    public ResultBean delbankcard(String bankcard) {
        if (tBankMapper.delbankcard(bankcard) > 0){
            return new ResultBean(200,"操作成功");
        }else{
            return new ResultBean(404,"操作失败");
        }

    }

}
